#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "tablemodel.h"
#include "delegate/comboboxdelegate.h"
#include "delegate/checkboxdelegate.h"

static const QStringList g_gradeNames = {"Grade 1", "Grade 2", "Grade 3", "Grade 4"};

class Item : public TableItem {
public:
    TableItem *base() { return static_cast<TableItem*>(this); }
    QVariant value(int section) const override {
        switch (section) {
        case No:
            return no;
        case Name:
            return name;
        case Sex:
            return men;
        case Age:
            return age;
        case Grade:
            return grade;
        default:
            break;
        }
        return QVariant();
    }

    QString valueString(int section) const override {
        if(section == Sex) {
            return men ? "Men" : "Women";
        }
        else if(section == Grade) {
            return g_gradeNames.at(grade);
        }
        return value(section).toString();
    }

    bool setValue(int section, const QVariant &value) override {
        switch (section) {
        case No:
            no = value.toString();
            break;
        case Name:
            name = value.toString();
            break;
        case Sex:
            men = value.toBool();
            break;
        case Age:
            age = value.toInt();
            break;
        case Grade:
            grade = value.toInt();
            break;
        default:
            break;
        }
        return true;
    }

    enum FiledType {
        No,
        Name,
        Sex,
        Age,
        Grade
    };

    QString no;
    QString name;
    bool men;
    int age;
    int grade;
};

MainWindow::MainWindow(
    QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_tableModel = new TableModel(this);
    ui->tableView->setModel(m_tableModel);
    ui->tableView_2->setModel(m_tableModel);

    ComboBoxDelegate *comboDelegate = new ComboBoxDelegate(g_gradeNames, this);
    ui->tableView->setItemDelegateForColumn(Item::Grade, comboDelegate);
    ui->tableView_2->setItemDelegateForColumn(Item::Grade, comboDelegate);
    CheckBoxDelegate *checBoxDelegate = new CheckBoxDelegate(this);
    ui->tableView->setItemDelegateForColumn(Item::Sex, checBoxDelegate);
    ui->tableView_2->setItemDelegateForColumn(Item::Sex, checBoxDelegate);

    ui->cbbGrade->addItems(g_gradeNames);

    QList<TableItem *> items;
    Item *item;
    item = new Item;
    item->no = "M1";
    item->name = "xiaoming";
    item->men = true;
    item->age = 10;
    item->grade = 1;
    items.push_back(static_cast<TableItem*>(item));
    item = new Item;
    item->no = "N1";
    item->name = "xiaohong";
    item->men = false;
    item->age = 12;
    item->grade = 3;
    items.push_back(static_cast<TableItem*>(item));

    QStringList headerNames = QStringList() << "No" << "Name" << "Sex" << "Age" << "Grade";

    QList<Qt::ItemFlags> flagList;
    flagList.push_back(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    flagList.push_back(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    flagList.push_back(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
    flagList.push_back(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    flagList.push_back(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
    m_tableModel->setHeaderNames(headerNames);
    m_tableModel->setItemFlags(flagList);
    m_tableModel->setItems(items);
}

MainWindow::~MainWindow()
{
    delete ui;
}




void MainWindow::on_btnAdd_clicked()
{
    Item *item;
    item = new Item;
    item->no = ui->leNo->text().trimmed();
    item->age = ui->sbAge->value();
    item->name = ui->leName->text().trimmed();
    item->men = ui->cbSex->isChecked();
    item->grade = ui->cbbGrade->currentIndex();
    m_tableModel->addItem(item->base());
}

